﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ConceptCorporateAdmin.Gui.Dialogs
{
    public enum ModAdministrare
    {
        Adaugare, Editare
    };

    public partial class ActNormativ : Form
    {  
        private ModAdministrare mod = ModAdministrare.Adaugare;
        private int id_for_edit  = -1;
        private int id_categorie = -1;
        DbActNormativ raspuns    = null;

        public ActNormativ( ModAdministrare mod, int id_categorie, int id_for_edit )
        {
            InitializeComponent();
            this.mod = mod;
            this.id_for_edit  = id_for_edit;
            this.id_categorie = id_categorie;
        }

        private void ActNormativ_Load(object sender, EventArgs e)
        {
            //incarca lista de tipuri acte normative
            DataTable dt            = DbTipuriActeNormative.Instance.Table;
            CmbTipAct.DataSource    = dt;
            CmbTipAct.DisplayMember = "Nume";
            CmbTipAct.ValueMember   = "Id";
            //arata valorile din db pt modul editare
            if (mod == ModAdministrare.Editare)
            {
                DbActNormativ act = new DbActNormativ();
                act.getFromId(id_for_edit);
                TxtNume.Text = act.Nume;
                TxtNumar.Value = act.Numar;
                CmbTipAct.SelectedValue  = act.IdTipActNormativ;
                DtDataPublicarii.Value   = DateTime.ParseExact(act.DataPublicarii,"dd/MM/yyyy", null);
                DtDataPublicariiMO.Value = DateTime.ParseExact(act.DataPublicariiMonitorOficial, "dd/MM/yyyy", null);
            }
            else
            {
                if (CmbTipAct.Items.Count > 0) CmbTipAct.SelectedIndex = 0;
            }
        }

        private void BtnRenunta_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            DbActNormativ act   = new DbActNormativ();
            if (mod == ModAdministrare.Editare) act.getFromId(id_for_edit);
            act.Id              = id_for_edit;
            act.IdCategorie     = id_categorie;
            act.DataPublicarii  = DtDataPublicarii.Value.ToString("dd/MM/yyyy");
            act.DataPublicariiMonitorOficial = DtDataPublicariiMO.Value.ToString("dd/MM/yyyy");
            act.IdTipActNormativ = (int)CmbTipAct.SelectedValue;
            act.Numar            = (int)TxtNumar.Value;
            act.Nume             = TxtNume.Text;

            if (mod == ModAdministrare.Adaugare)
                act.CreateNew();
            else
                act.Update();
            raspuns = act;
            Close();
        }

        public static DbActNormativ ShowNew(int id_categorie)
        {
            ActNormativ dlg = new ActNormativ(ModAdministrare.Adaugare, id_categorie, -1);
            dlg.ShowDialog();
            return dlg.raspuns;
        }

        public static DbActNormativ ShowEdit(int id_categorie, int id_for_edit)
        {
            ActNormativ dlg = new ActNormativ(ModAdministrare.Editare, id_categorie, id_for_edit);
            dlg.ShowDialog();
            return dlg.raspuns;
        }

        private void CmbTipAct_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
